package com.msos.servlet.BQueryServlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msos.model.business.Business;
import com.msos.model.customer.Customer;
import com.msos.model.respBean.RespBean;
import com.msos.service.BQueryService.BQueryService;
import com.msos.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: BQueryServlet
 * @Description: TODO
 * @Author: hsx
 * @Date: 2022/4/14 9:15
 * @Version: v1.0
 */
@WebServlet(urlPatterns = "/bquery")
public class BQueryServlet extends HttpServlet {
    BQueryService bQueryService = new BQueryService();

    //获取页面或页面展示数据
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if ("page".equals(action)){
            req.getRequestDispatcher("/WEB-INF/jsp/bQuery/bQuery.jsp").forward(req,resp);
        }else if ("data".equals(action)){
            Customer customer = (Customer) req.getSession().getAttribute("loginCustomer");
            List<Business> business = bQueryService.getAllBusiness(customer);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(business));
        }else if ("data_from_manager".equals(action)){
            Customer customer = (Customer) req.getSession().getAttribute("loginCustomer");
            List<Business> business = bQueryService.getAllBusinesses(customer);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(business));
        }
    }

    //办理业务
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bid = req.getParameter("bid");
        Customer customer = (Customer) req.getSession().getAttribute("loginCustomer");
        Integer result = bQueryService.addBusiness(bid,customer);
        RespBean respBean = null;
        if (result == CommonsUtils.EXCEPTION_ERROR){
            respBean = RespBean.error("办理失败！");
        }else {
            respBean = RespBean.ok("办理成功！");
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }

    //取消业务
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bid = req.getParameter("bid");
        Integer result = bQueryService.deleteBusiness(bid);
        RespBean respBean = null;
        if (result == CommonsUtils.EXCEPTION_ERROR){
            respBean = RespBean.error("取消失败！");
        }else {
            respBean = RespBean.ok("取消成功！");
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }
}
